﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SCCN.DAL;

namespace SCCN.BLL
{
    public class AdSpaceBLL
    {
        private static AdSpaceBLL instance = null;

        /// <summary>
        /// 单例构造
        /// </summary>
        /// <returns></returns>
        public static AdSpaceBLL getInstance()
        {
            if (instance == null)
            {
                instance = new AdSpaceBLL();
            }
            return instance;
        }

        public bool Add(SC_AdSpace obj)
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                entites.AddToSC_AdSpace(obj);
                return entites.SaveChanges() > 0 ? true : false;
            }
        }

        public bool Update(SC_AdSpace obj)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                SC_AdSpace objAdSpace = entities.SC_AdSpace.FirstOrDefault(v => v.Id == obj.Id);
                entities.Detach(objAdSpace);
                objAdSpace.ParentId = obj.ParentId;
                objAdSpace.AdSpaceName = obj.AdSpaceName;
                objAdSpace.IsDelete = obj.IsDelete;
                objAdSpace.AdSpaceDesc = obj.AdSpaceDesc;
                entities.Attach(objAdSpace);
                var newAdSpace = entities.ObjectStateManager.GetObjectStateEntry(objAdSpace);
                newAdSpace.SetModified();
                newAdSpace.SetModifiedProperty("ParentId");
                newAdSpace.SetModifiedProperty("AdSpaceName");
                newAdSpace.SetModifiedProperty("IsDelete");
                newAdSpace.SetModifiedProperty("AdSpaceDesc");
                return entities.SaveChanges() > 0 ? true : false;
            }
        }

        public bool Delete(int id)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                SC_AdSpace obj = entities.SC_AdSpace.FirstOrDefault(r => r.Id == id);
                obj.IsDelete = true;
                return entities.SaveChanges() > 0 ? true : false;
            }
        }

        public SC_AdSpace GetAdSpaceById(int id)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_AdSpace.FirstOrDefault(r => r.IsDelete == false && r.Id == id);
            }
        }

        /// <summary>
        /// 根据广告位查找
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public List<SC_AdSpace> GetAdSpacesByName(string adSpaceName)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_AdSpace.Where(r => r.IsDelete == false && r.AdSpaceName.Contains(adSpaceName)).ToList<SC_AdSpace>();
            }
        }


        /// <summary>
        /// 根据广告位查找
        /// </summary>
        /// <param name="categoryName"></param>
        /// <returns></returns>
        public SC_AdSpace GetAdSpaceByName(string adSpaceName)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_AdSpace.FirstOrDefault(r => r.IsDelete == false && r.AdSpaceName == adSpaceName);
            }
        }

        public List<SC_AdSpace> GetAdSpaceByParentId(int parentId)
        {
            using (SCCNEntities entities = new SCCNEntities())
            {
                return entities.SC_AdSpace.Where(r => r.IsDelete == false && r.ParentId == parentId).ToList<SC_AdSpace>();
            }
        }


        /// <summary>
        /// 根据条件查询广告位
        /// </summary>
        /// <returns></returns>
        public List<SC_AdSpace> GetAdSpaceByCondition(SC_AdSpace obj, int pageIndex, int pageSize, out int rowCount)
        {
            using (SCCNEntities entites = new SCCNEntities())
            {
                rowCount = 0;
                var list = from r in entites.SC_AdSpace.Where(a => a.IsDelete == false)
                           select r;
                if (obj.AdSpaceName.Length > 0)
                {
                    list = list.Where(a => a.AdSpaceName.Contains(obj.AdSpaceName));
                }
                if (obj.ParentId >= 0)
                {
                    list = list.Where(a => a.ParentId == obj.ParentId);
                }
                if (pageSize > 0)
                {
                    rowCount = list.Count<SC_AdSpace>();
                    list = list.OrderBy(a => a.Id).Skip((pageIndex - 1) * pageSize).Take(pageSize);
                }
                return list.ToList<SC_AdSpace>();
            }
        }
    }
}
